package wiki.zimo.dao;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import wiki.zimo.domain.Order;

import java.sql.SQLException;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/5 17:48
 * @description: OrderDao数据库操作类
 */
public class OrderDao extends BaseDao<Order> {

    /**
     * @auther: 子墨
     * @date: 2018/11/6 22:47
     * @describe: 根据用户id查找对应订单集
     * @param: [uid]
     * @return: java.util.List<wiki.zimo.domain.Order>
     * @version v1.0
     */
    public List<Order> findOrdersByUid(String uid) throws SQLException {
        String sql = "select * from `order` where uid = ?";
        return qr.query(sql, new BeanListHandler<>(Order.class), uid);
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 20:54
     * @describe: 统计用户订单数
     * @param: [uid]
     * @return: int
     * @version v1.0
     */
    public int countByUid(String uid) throws SQLException {
        return findOrdersByUid(uid).size();
    }

}
/*public class OrderDao implements Dao<Order,String> {

	// 获取SQL语句的内部类
	class SQL extends SQLUtils<Order> {

	}
	// 获取SQL语句的对象
	SQL s = new SQL();

	@Override
	public Order insert(Order entity) throws SQLException {
//		String sql = "insert into `order` values( ?, ?, ?, ?, ?, ?, ?, ? )";
		String sql = s.getInsertSql();
		return qr.insert(sql,
				new BeanHandler<>(Order.class),
				entity.getOid(),
				entity.getUid(),
				entity.getOrdertime(),
				entity.getTotal(),
				entity.getState(),
				entity.getAddress(),
				entity.getName(),
				entity.getTelephone()
		);
	}

	@Override
	public boolean delete(Order entity) throws SQLException {
//		String sql = "delete from `order` where oid = ?";
		String sql = s.getDeleteSql();
		int update = qr.update(sql, entity.getOid());
		if (update > 0){
			return true;
		}
		return false;
	}

	@Override
	public Order update(Order entity) throws SQLException {
//		String sql = "update `order` set uid = ?, ordertime = ?, total = ?, state = ?,address = ?, name = ?, telephone = ? where oid = ?";
		String sql = s.getUpdateSql();
		int update = qr.update(sql,
				entity.getUid(),
				entity.getOrdertime(),
				entity.getTotal(),
				entity.getState(),
				entity.getAddress(),
				entity.getName(),
				entity.getTelephone(),
				entity.getOid()
		);
		if (update > 0){
			return findOneById(entity.getOid());
		}
		return null;
	}

	@Override
	public Order findOneById(String id) throws SQLException {
//		String sql = "select * from `order` where oid = ? limit 1";
		String sql = s.getFindOneByIdSql();
		return qr.query(sql,new BeanHandler<>(Order.class),id);
	}

	@SuppressWarnings("NoUse")
	@Override
	public Order findOneByParams(String... params) throws SQLException {
		return null;
	}

	@Override
	public List<Order> findAll() throws SQLException {
//		String sql = "select * from `order`";
		String sql = s.getFindAllSql();
		return qr.query(sql,new BeanListHandler<>(Order.class));
	}

	@Override
	public int count() throws SQLException {
		return findAll().size();
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/6 22:47
 * @describe: 根据用户id查找对应订单集
 * @param: [uid]
 * @return: java.util.List<wiki.zimo.domain.Order>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/9 20:54
 * @describe: 统计用户订单数
 * @param: [uid]
 * @return: int
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/9 20:54
 * @describe: 统计用户订单数
 * @param: [uid]
 * @return: int
 * @version v1.0
 *//*
	public List<Order> findOrdersByUid(String uid) throws SQLException {
		String sql = "select * from `order` where uid = ?";
		return qr.query(sql,new BeanListHandler<>(Order.class),uid);
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/9 20:54
 * @describe: 统计用户订单数
 * @param: [uid]
 * @return: int
 * @version v1.0
 *//*
	public int countByUid(String uid) throws SQLException {
		return findOrdersByUid(uid).size();
	}
}*/
